package com.hlx.herostory.cmdhandler;

import com.hlx.herostory.Broadcaster;
import com.hlx.herostory.model.User;
import com.hlx.herostory.model.UserManager;
import com.hlx.herostory.msg.GameMsgProtocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;

public class UserAttkCmdHandler implements ICmdHandler<GameMsgProtocol.UserAttkCmd> {
    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.UserAttkCmd msg) {
        int subtractHp = 10;

        if (null == ctx ||
                null == msg) {
            return;
        }

        Integer attkUserId = (Integer) ctx.channel().attr(AttributeKey.valueOf("userId")).get();
        if (attkUserId == null) {
            return;
        }

        // 获取被攻击者id
        int targetUserId = msg.getTargetUserId();
        User targetUser = UserManager.getByUserId(targetUserId);
        if (targetUser == null) {
            broadAttk(attkUserId, -1);
            return;
        }

        // 攻击
        broadAttk(attkUserId, targetUserId);

        if (targetUser.currHp <= 0) {
            // 死亡
            broadcastDie(targetUserId);
        } else {
            // 减血
            targetUser.currHp = targetUser.currHp - subtractHp;
            broadcastSubtractHp(targetUserId, subtractHp);
        }

    }

    /**
     * 广播攻击
     *
     * @param attkUserId
     * @param targetUserId
     */
    static private void broadAttk(int attkUserId, int targetUserId) {
        // 攻击
        GameMsgProtocol.UserAttkResult.Builder resultBuilder = GameMsgProtocol.UserAttkResult.newBuilder();
        resultBuilder.setAttkUserId(attkUserId);
        resultBuilder.setTargetUserId(targetUserId);
        GameMsgProtocol.UserAttkResult newResult = resultBuilder.build();
        Broadcaster.broadcast(newResult);
    }

    /**
     * 广播减血消息
     *
     * @param targetUserId 被攻击者 Id
     * @param subtractHp   减血量
     */
    static private void broadcastSubtractHp(int targetUserId, int subtractHp) {
        if (targetUserId <= 0 ||
                subtractHp <= 0) {
            return;
        }

        GameMsgProtocol.UserSubtractHpResult.Builder resultBuilder = GameMsgProtocol.UserSubtractHpResult.newBuilder();
        resultBuilder.setTargetUserId(targetUserId);
        resultBuilder.setSubtractHp(subtractHp);

        GameMsgProtocol.UserSubtractHpResult newResult = resultBuilder.build();
        Broadcaster.broadcast(newResult);
    }

    /**
     * 广播死亡消息
     *
     * @param targetUserId 被攻击者 Id
     */
    static private void broadcastDie(int targetUserId) {
        if (targetUserId <= 0) {
            return;
        }

        GameMsgProtocol.UserDieResult.Builder resultBuilder = GameMsgProtocol.UserDieResult.newBuilder();
        resultBuilder.setTargetUserId(targetUserId);

        GameMsgProtocol.UserDieResult newResult = resultBuilder.build();
        Broadcaster.broadcast(newResult);
    }
}
